package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.SysDeptConvert;
import com.hsj.common.system.entity.SysDeptDo;
import com.hsj.common.system.service.ISysDeptService;
import com.hsj.common.system.vo.request.ReqSysDeptCreateVo;
import com.hsj.common.system.vo.request.ReqSysDeptUpdateVo;
import com.hsj.common.system.vo.request.ReqSysDeptPageQueryVo;
import com.hsj.common.system.vo.response.RespSysDeptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 部门信息 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "部门信息管理")
@RestController
@RequestMapping("/sysDept")
@Slf4j
public class SysDeptController {

    private final ISysDeptService sysDeptService;
    private final SysDeptConvert sysDeptConvert;

    public SysDeptController(ISysDeptService sysDeptService, SysDeptConvert sysDeptConvert) {
        this.sysDeptService = sysDeptService;
        this.sysDeptConvert = sysDeptConvert;
    }

    @PostMapping("createSysDept")
    @ApiOperation(value = "创建部门信息", notes = "创建部门信息", httpMethod = "POST")
    public Serializable createSysDept(@Valid @RequestBody ReqSysDeptCreateVo vo){
        return sysDeptService.insert(sysDeptConvert.voToDo(vo));
    }

    @PostMapping("updateSysDept")
    @ApiOperation("修改部门信息")
    public Serializable updateSysDept(@Valid @RequestBody ReqSysDeptUpdateVo vo){
        return sysDeptService.update(sysDeptConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除部门信息")
    public Boolean delete(@PathVariable("id")String id){
        return sysDeptService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询部门信息")
    public Pager<RespSysDeptVo> pageList(@Valid @RequestBody ReqSysDeptPageQueryVo vo){
        Pager<SysDeptDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = sysDeptService.getPager(pager, vo.getSmCriteria());
        pager.setResult(sysDeptConvert.doToVo(pager.getResult()));
        Pager<RespSysDeptVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取部门信息信息")
    public RespSysDeptVo get(@PathVariable("id")String id) {
        return sysDeptConvert.doToVo(sysDeptService.get(id));
    }
}
